package ca.etsmtl.log430.lab2;
/**
 * This class will read from the InputFile and instantiate the Course objects
 * in the system. It is assumed that the InputFile is in the local directory,
 * contains the scheduled courses and each line of input is read and expected
 * in the following format.
 * <pre>
 *		LOG430 1 LME 1030 1200 Champagne  Architecture logicielle
 *		|      | |   |    |    |          |
 *		|      | |   |    |    Instructor Course Title
 *		|      | |   |    Stop Time
 *		|      | |   Start Time
 *		|      | Days
 *		|      Group
 *		Course Number </pre>
 *
 * The coursLOG.txt file has been provided as an example file.
 *
 * @author A.J. Lattanze, CMU
 * @version 1.2, 2011-Feb-02
 */

/* Modification Log
 *****************************************************************************
 * v1.2, 2011-Feb-02, R. Champagne - Various refactorings, javadoc comments.
 *  
 * v1.1, 2002-May-21, R. Champagne - Adapted for use at ETS. 
 * 
 * v1.0, 12/29/99, A.J. Lattanze - Original version.
 *****************************************************************************/

public class CourseReader extends LineOfTextFileReader {

	/**
	 * List of courses.
	 */
    private CourseList listOfCourses = new CourseList();

    public CourseReader() {

        setListOfCourses(null);

    } // Constructor #1

    public CourseReader(String InputFile) {

        setListOfCourses(getCourseList(InputFile));

    } // Constructor #2

	/**
	 * Reads a line of text from the course file. The line of text is then
	 * passed to the ParseCourseText method where it is parsed and a Course
	 * object is returned. The Course object is then added to the list. When a
	 * null is read from the Course file the method terminates and returns the
	 * list to the caller. A list that points to null is an empty list.
	 * 
	 * @param InputFile
	 * @return The CourseList properly populated
	 */
    public CourseList getCourseList(String InputFile) {

        String CourseText;
        boolean Done;
        CourseList ListObject = new CourseList();

        if (openFile(InputFile)) {

            Done = false;

            while (!Done) {

                try {

                    CourseText = readLineOfText();

                    if (CourseText == null) {

                        Done = true;

                    } else {

                        ListObject.addCourse(parseCourseText(CourseText));

                    } // if 

                } // try

                catch (Exception Error) {

                    return (null);

                } // catch

            } // while		

        } else {

            return (null);

        } // if

        closeFile();

        return (ListObject);

    } // GetCourseList

	/**
	 * Parse lines of text that are read from the text file containing course
	 * information in the above format. Note that this is a private method and
	 * it is used exclusively by getCourseList.
	 * 
	 * @param LineOfText
	 * @return A properly populated Course instance.
	 */
    private Course parseCourseText(String LineOfText) {

        boolean Done; // Loop terminator
        String Token; // String token parsed from LineOfText
        int TokenCount; // Number of tokens parsed
        int FrontIndex; // Front index of token to parse
        int BackIndex; // Back index of token to parse

        // Instantiate a course object

        Course NewCourse = new Course();

        TokenCount = 0;
        FrontIndex = 0;
        BackIndex = 0;
        Done = false;

        while (!Done) {

            BackIndex = LineOfText.indexOf(' ', FrontIndex);

            if (TokenCount < 6) {

                Token = LineOfText.substring(FrontIndex, BackIndex);

            } else {

                Token = LineOfText.substring(FrontIndex);

            } // if 

            switch (TokenCount) {

                case 0 : // Course number 
                    NewCourse.setCourseID(Token);
                    FrontIndex = BackIndex + 1;
                    TokenCount++;
                    break;

                case 1 : // Course Section ID
                    NewCourse.setSection(Token);
                    FrontIndex = BackIndex + 1;
                    TokenCount++;
                    break;

                case 2 : // Days of the week the course will be held on
                    NewCourse.setDays(Token);
                    FrontIndex = BackIndex + 1;
                    TokenCount++;
                    break;

                case 3 : // Course start time
                    NewCourse.setStartTime(Token);
                    FrontIndex = BackIndex + 1;
                    TokenCount++;
                    break;

                case 4 : // Course stop time
                    NewCourse.setStopTime(Token);
                    FrontIndex = BackIndex + 1;
                    TokenCount++;
                    break;

                case 5 : // Course instructor
                    NewCourse.setInstructor(Token);
                    FrontIndex = BackIndex + 1;
                    TokenCount++;
                    break;

                case 6 : // Course title or name of the course
                    NewCourse.setCourseName(Token);
                    Done = true;
                    break;

            } // end switch

        } // end while

        return (NewCourse);

    } // ParseCourseText

	public void setListOfCourses(CourseList listOfCourses) {
		this.listOfCourses = listOfCourses;
	}

	public CourseList getListOfCourses() {
		return listOfCourses;
	}

} // CourseReader
